<?php
namespace backend\controllers;
use Yii;
use yii\web\Controller;
use backend\Models\Admin;
/**
 * Site controller
 */
class IndexController extends Controller
{
   public $enableCsrfValidation = false;//解决400错误
   // 注册页面
   public function actionIndex(){
    
   	      $model=new Admin();//模型
   	      $model->setScenario('look');//设置场景

   	 if($model->load(Yii::$app->request->post())&&$model->validate()){

   	 	    $model->password=Sha1($model->password);//密码加密
          $model->dtime=date("Y-m-d H-i-s",time());//当前时间

   	 	 if($model->insert(false)){//添加数据

            Yii::$app->getSession()->setFlash('success',"注册成功");
            return $this->redirect('index.php?r=index/login');//成功跳转路径

           }else{	//失败原页面

                return $this->render('add',['model'=>$model]);
             }
   	  }else{
            //指向view
   	        return $this->render('add',['model'=>$model]); 
   	      }
   }

// 登录页面
  public function actionLogin(){

      $model=new Admin();
      $model->setScenario('logo');//使用场景

      // 接值并去验证模型规则
      if($model->load(Yii::$app->request->post())&&$model->validate())
      {
          if($model->on_logo()){
                     //指向模型中做出的方法判断        
               return $this->redirect('index.php?r=index/good');

              }else{

                  Yii::$app->getSession()->setFlash('error',"用户名或密码错误");
                  return $this->render('login',['model'=>$model]);
              }
              
        }else{
      	        return $this->render('login',['model'=>$model]);
            }
     }

// 验证码
      public function actions(){
           return [
             'error' => [
             'class' => 'yii\web\ErrorAction',
            ],
             'captcha' => [
             'class' => 'yii\captcha\CaptchaAction',
             'maxLength'=>3,
             'minLength'=>3,
            	],
        	];
      }


//跳转成功页面;
      public function actionGood(){

          $model=new Admin();
          $session=Yii::$app->session;//打开session

       	  echo "欢迎"."<font color='red'>".$session['username']."<font>";
         
       }
}